﻿using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Threading;

namespace ClockWebSocket
{
    public class ClockHub : Hub
    {
        private bool isRunning = true;
        private Dictionary<string, TimeZoneInfo> timezonesDictioanry;
        private TimeZoneInfo currentTimezone;
        private const string curDate = "dd MMMM yyyy";
        private const string curTime = "hh:mm:ss.f tt zzz";

        public ClockHub()
        {
            currentTimezone = TimeZoneInfo.Local;
            timezonesDictioanry = new Dictionary<string, TimeZoneInfo>();
            foreach (TimeZoneInfo tmz in TimeZoneInfo.GetSystemTimeZones())
            {
                timezonesDictioanry.Add(tmz.DisplayName, tmz);
            }
        }

        public void Send()
        {
            while (isRunning)
            {
                DateTime date = TimeZoneInfo.ConvertTime(DateTime.Now, this.currentTimezone);
                // Call the broadcastMessage method to update clients.
                Clients.Client(this.Context.ConnectionId).broadcastMessage("Time: " + date.ToString(curTime) +
                    "  Date: " + date.ToString(curDate));
                Thread.Sleep(80);
            }
        }
    }
}